package com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.dto.FxqhAssessReportTemplateAddDto;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.dto.FxqhAssessReportTemplateUpdateDto;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.vo.ReportTemplateDetailVo;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.service.FxqhAssessReportTemplateService;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 报告模板管理
 */
@RestController
@RequestMapping("assessReportTemplate")
public class FxqhAssessReportTemplateController {

    @Autowired
    private FxqhAssessReportTemplateService fxqhAssessReportTemplateService;

    /**
     * 生成模板编号
     *
     * @return
     */
    @GetMapping("buildTemplateCode")
    public RestResponse<String> buildTemplateCode() {
        return RestResponse.succeed(fxqhAssessReportTemplateService.buildTemplateCode());
    }

    /**
     * 新增模板
     *
     * @param dto
     * @return
     */
    @PostMapping("add")
    public RestResponse<?> add(@Valid @RequestBody FxqhAssessReportTemplateAddDto dto) throws BizException {
        if (dto.getFileId().split(",").length > 1) {
            return RestResponse.fail("只能上传一个模板");
        }
        fxqhAssessReportTemplateService.add(dto);
        return RestResponse.succeed();
    }

    /**
     * 列表查询
     *
     * @param code
     * @param name
     * @return
     */
    @GetMapping("list")
    public RestResponse<?> list(String code, String name) {
        return RestResponse.succeed(fxqhAssessReportTemplateService.list(code, name));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("delete")
    public RestResponse<?> delete(@RequestParam String id) {
        fxqhAssessReportTemplateService.delete(id, PlatformSessionContext.getUserID());
        return RestResponse.succeed();
    }

    /**
     * 上传
     *
     * @param templateId
     * @param fileId
     * @return
     */
    @GetMapping("upload")
    public RestResponse<?> upload(@RequestParam String templateId, @RequestParam String fileId) {
        if (fileId.split(",").length > 1) {
            return RestResponse.fail("只能上传一个模板");
        }
        fxqhAssessReportTemplateService.upload(templateId, fileId, PlatformSessionContext.getUserID());
        return RestResponse.succeed();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public RestResponse<ReportTemplateDetailVo> detail(@RequestParam String id) {
        return RestResponse.succeed(fxqhAssessReportTemplateService.detail(id));
    }

    @PostMapping("update")
    public RestResponse<?> update(@Valid @RequestBody FxqhAssessReportTemplateUpdateDto dto) throws BizException {
        if (dto.getFileId().split(",").length > 1) {
            return RestResponse.fail("只能上传一个模板");
        }
        String uid = PlatformSessionContext.getUserID();
        if (StringUtils.isBlank(uid)) {
            return RestResponse.fail("请先登录");
        }
        fxqhAssessReportTemplateService.update(dto, uid);
        return RestResponse.succeed();
    }
}
